#include<stdio.h>
#include "../include/vfs.h"
#include<stdlib.h>
#include<string.h>


int makedir(char parent[], char dir[])
{
	making=0;
	FILE *fp;
	int i,last=0;
	FD *fd;
	char *c,e[100]="",f[2]="/",check[100];

	if(!valid_name(dir))
		return 2;

	if(parent[0]!='/')
		return 1;

	fp = fopen(filesystem_name,"r+b");

	header=malloc(sizeof(Header));

	fread(header,sizeof(Header),1,fp);

	if(strcmp(parent,"/")!=0)
	{
		if(parent[strlen(parent)-1]=='/')
		{
			strcat(parent,dir);

		}

		else
		{
			strcat(parent,"/");
			strcat(parent,dir);

		}

	}

	else
		strcat(parent,dir);	

	//printf("\n PATH:%s",parent);

	strcpy(check,parent);

	c=strtok(parent,"/");
	
		while(c!=NULL){

			strcat(e,f);
			strcat(e,c);

			//printf("\n E:%s",e);
			//printf("\n C:%s",c);

			if(strcmp(check,e)==0)
			{
				last=1;
			}

			if(strcmp(e,"/")!=0)
			{

				for(i=1;i<header->max_FD;i++)
				{
					if(header->fd_list[i].block_no==0)
					{
						fd=(FD*)malloc(sizeof(FD));
						strcpy(fd->filename,c);
						strcpy(fd->path,e);
						fd->file_type =0;
						fd->block_no=-1;
						fd->file_size=0;
	
						break;
					}
				}
	
	
				roots.naryroot=add_node(roots.naryroot,fd,last);
				roots.bstRoot=insert(roots.bstRoot,fd);	
			
			}
	
					c=strtok(NULL,"/");
	}
	rewind(fp);
	fwrite(header,sizeof(Header),1,fp);

	fclose(fp);

	if(making==1)
	return 0;
	else
	return 3;
}



